<template>
	<view class="container">
		<u-navbar title='咨询问卷' :title-bold='true' :border-bottom='false'></u-navbar>
		<u-search bg-color='#FFF' @change="search" margin="0" class="search" placeholder="搜索姓名" :show-action="false"
			v-model="keyword"></u-search>
		<view class="topic">
			<view @click="select(index)" :class="index==t_index?'select':''" class="t-item"
				v-for="(item,index) in topic" :key="index">
				<span>{{item}}</span>
			</view>
		</view>

		<view class="card" v-for="(item,idx) in list" :key="idx">
			<view class="top" @click="goTo('/sub/center/mine/zixunDetail?id='+item.id)">
				<view class="c-t">
					<image src="@/static/img/mine/yiyuan.png" mode="widthFix"></image>
					<span>{{item.sn}}</span>
				</view>
				<span class="statu">{{item.status_text}}</span>
			</view>
			<u-line></u-line>
			<view class="center">
				<view class="userInfo" v-if="user.job==2" @click="goTo('/sub/center/mine/zixunDetail?id='+item.id)">
					<u-avatar :size='128' mode="square" :src="item.user_info.avatar"></u-avatar>
					<view class="dsc">
						<view class="d1">
							<!-- 医师 -->
							<span class="s1">{{item.user_info.username}}</span>
							<span>{{item.user_info.speciality}}</span>
						</view>
						<view class="d2">
							<span class="s1"></span>
							<!-- 患者 -->
							<span>{{item.member_info.username}}</span>
							<span>{{item.member_info.gender_text}}</span>
							<span>{{item.member_info.age}}</span>
						</view>
						<view class="d2">
							<span class="s1"></span>
							<span>{{item.createday}}</span>
						</view>
					</view>
				</view>
				<view class="userInfo" @click="goTo('/sub/center/mine/zixunDetail?id='+item.id)" v-else>
					<u-avatar :size='128' mode="square" :src="item.doctor_info.avatar"></u-avatar>
					<view class="dsc">
						<view class="d1">
							<!-- 医师 -->
							<span class="s1">{{item.doctor_info.username}}</span>
							<span>{{item.doctor_info.speciality}}</span>
						</view>
						<view class="d2">
							<span class="s1"></span>
							<!-- 患者 -->
							<span>{{item.member_info.username}}</span>
							<span>{{item.member_info.gender_text}}</span>
							<span>{{item.member_info.age}}</span>
						</view>
						<view class="d2">
							<span class="s1"></span>
							<span>{{item.createday}}</span>
						</view>
					</view>
				</view>

				<view class="button">
					<!-- 用户端 -->
					<span v-if="item.status==0 && user.job==1" @click.stop="cancel(item.id)">取消咨询</span>
					<span v-if="item.status==0 && user.job==2"
						@click="goTo('/sub/center/mine/zixunDetail?id='+item.id)">答复</span>
					<span v-if="item.status==1 && user.job==2" @click.stop="follow(item.id,item.user_id)">回访</span>
					<span v-if="item.status==1"
						@tap.stop="goTo('/sub/prescription/index?id='+item.prescription_id)">查看处理方法</span>
					<!-- 护航员端 -->
					<span v-if="user.job==2" @click.stop="tochat(item.user_id)">联系用户</span>
				</view>
			</view>
		</view>
		<view class="notDta" v-if="list.length<1">
			暂无数据
		</view>
		<!-- 提示 -->
		<u-toast ref="uToast" />
	</view>
</template>

<script>
	import {
		userList,
		doctorList,
		zixunCancel,
		chat,
		addfollow
	} from '@/api/index.js'
	export default {
		data() {
			return {
				list: [],
				topic: ['全部', '待处理', '已处理'],
				t_index: 0,
				keyword: '',
				page: 1,
				user: {},
				other: '',
				status: ''
			}
		},
		onLoad(e) {
			if (e.id) {
				this.other = e.id
			}
		},
		onShow() {
			this.page = 1
			this.list = []
			this.init()
		},
		onReachBottom() { //监听页面触底函数
			this.init()
		},
		methods: {
			follow(id, user) {
				addfollow({
					advice_id: id
				}).then(res => {
					if (res.code == 1) {
						this.tochat(user)
					} else {
						uni.showToast({
							title: res.msg,
							icon: 'error'
						})
					}
				})
			},
			init() {
				let that = this;
				that.user = that.ws.getUserInfo()
				let obj = {
					'page': that.page,
					'per_page': 10,
					'see_user_id': that.other,
					'status': that.status,
					'keyword': that.keyword
				}
				if (that.user.job == 2) {
					doctorList(obj).then(res => {
						if (res.code == 1) {
							that.list = [...that.list, ...res.data.list.data]
							if (res.data.list.data.length > 0) {
								that.page++
							}
						} else {
							that.$refs.uToast.show({
								title: res.msg
							})
						}

					})
				} else {
					userList(obj).then(res => {
						if (res.code == 1) {
							that.list = [...that.list, ...res.data.list.data]
							if (res.data.list.data.length > 0) {
								that.page++
							}
						} else {
							that.$refs.uToast.show({
								title: res.msg
							})
						}

					})
				}

			},
			select(idx) {
				this.t_index = idx
				if (idx) {
					this.status = idx - 1
				} else {
					this.status = ''
				}
				this.page = 1
				this.list = []
				this.init()
			},
			search() {
				this.list = []
				this.page = 1
				this.init()
			},

			cancel(id) {
				let that = this
				zixunCancel({
					'id': id
				}).then(res => {
					if (res.code == 1) {
						that.list = []
						that.page = 1
						that.init()
					} else {
						that.$refs.uToast.show({
							title: res.msg
						})
					}
				})
			},
			goTo(url) {
				uni.navigateTo({
					url: url
				})
			},
			tochat(id) {
				let that = this
				uni.showLoading({
					title: '正在建立会话'
				});
				chat({
					'user_id': id
				}).then(res => {
					uni.hideLoading()
					if (res.code == 1) {
						that.ws.tochat(res.data.session_id)
					}
				})
			}

		}
	}
</script>

<style lang="less" scoped>
	.container {
		height: 100vh;
		padding: 15px;

		.u-nav-slot {
			display: flex;
		}

		.topic {
			display: flex;
			justify-content: space-around;
			margin: 20rpx 0;
			color: #3D3D3D;
			font-size: 16px;

			.t-item {
				text-align: center;
				padding: 16rpx 20rpx;
			}

			.select {
				border-bottom: 2px solid #6EC9E1;
				color: #6EC9E1;
				font-weight: bold;
			}
		}

		.notDta {
			height: 60px;
			line-height: 60px;
			text-align: center;
			margin-top: 40rpx;
		}

		.card {
			margin-top: 20rpx;
			background-color: #FFF;
			border-radius: 20rpx;

			.top {
				display: flex;
				justify-content: space-between;
				font-size: 13px;
				padding: 30rpx;

				.c-t {
					image {
						height: 14px;
						width: 14px;
						margin-right: 10rpx;
					}
				}
			}

			.center {
				padding: 30rpx;

				.button {
					display: flex;
					justify-content: flex-end;
					z-index: 999;

					span {
						border-radius: 30rpx;
						opacity: 0.7;
						min-width: 75px;
						border: 1rpx solid #8d8d8d;
						margin-left: 20rpx;
						text-align: center;
						height: 30px;
						line-height: 30px;
					}
				}

				.userInfo {
					display: flex;
					align-items: center;

					.dsc {
						margin-left: 16rpx;
						line-height: 20px;

						span {
							opacity: 0.6;
							font-size: 12px;
						}

						.d1 {
							.s1 {
								margin-right: 10rpx;
								font-size: 16px;
								font-weight: 550;
								opacity: 1;
							}
						}

						.d2 {
							display: flex;
							align-items: center;

							span {
								margin-right: 10rpx;
							}

							.s1 {
								display: block;
								background-color: #151515;
								border-radius: 50%;
								height: 10rpx;
								width: 10rpx;
							}
						}

					}
				}
			}
		}

	}
</style>